<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item prop="examineUserName">
        <el-input
          v-model="queryParams.examineUserName"
          placeholder="请输入考核人姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item prop="examineCycle">
        <el-select v-model="queryParams.examineCycle" placeholder="请选择考核周期" clearable @change="getCycleValue">
          <el-option
            v-for="dict in dict.type.examine_cycle"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item v-if="cycleVal==='month'" prop="pickCycle">
        <el-date-picker
          v-model="queryParams.pickCycle"
          type="month"
          placeholder="选择周期"
          style="width: 100%"
          format="yyyy 年 MM 月"
          value-format="yyyy-MM-dd"
          :picker-options="examineYearOptions"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item v-if="cycleVal==='quarter'" prop="pickCycle">
        <quarter v-model="queryParams.pickCycle" @change="getQuarter"></quarter>
      </el-form-item>
      <el-form-item v-if="cycleVal==='half_year'" prop="pickCycle">
        <halfyear v-model="queryParams.pickCycle" @change="getHalfyear"></halfyear>
      </el-form-item>
      <el-form-item v-if="cycleVal==='year'" prop="pickCycle">
        <el-date-picker
          v-model="queryParams.pickCycle"
          type="year"
          style="width: 100%"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item prop="examineResult">
        <el-select v-model="queryParams.examineResult" placeholder="考核结果" clearable>
          <el-option
            v-for="dict in dict.type.examine_standard"
            :key="dict.value"
            :label="dict.label"
            :value="Number(dict.value)"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button
          type="danger"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['examine:user:list']"
        >导出
        </el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="commonList">
      <el-table-column label="被考核人名称" align="center" prop="userName"/>
      <el-table-column label="所在部门" align="center" prop="ancestorNames"/>
      <el-table-column label="考核周期" align="center" prop="pickCycleText" width="130px"/>
      <el-table-column label="考核结果" align="center" prop="examineResult">
        <template slot-scope="scope">
          {{ getDictValue(scope.row.examineResult) }}
        </template>
      </el-table-column>
      <el-table-column label="考核人姓名" align="center" prop="examineUserName"/>
      <el-table-column label="考核时间" align="center" prop="examineTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.examineTime) }}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import quarter from "@/views/tool/quarter";
import halfyear from "@/views/tool/halfyear";
import {listExamineUser} from "@/api/examine/user/api";

export default {
  name: "Common",
  dicts: ['examine_cycle', 'examine_standard'],
  components: {
    quarter,
    halfyear,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      cycleVal: '',
      // 通用考核表格数据
      commonList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        examineUserName: undefined,
        examineCycle: undefined,
        pickCycle: undefined,
        examineResult:undefined
      },
      examineYearOptions: {
        disabledDate: (time) => {
          const date = new Date()
          const year = date.getFullYear()
          const timeYear = time.getFullYear()
          return year > timeYear + 3
        }
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询通用考核列表 */
    getList() {
      this.loading = true;
      listExamineUser(this.queryParams).then(response => {
        this.commonList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getDictValue(data) {
      const obj = this.dict.type.examine_standard.find(d => Number(d.value) === data)
      if (obj) {
        return obj.label
      }
    },
    getCycleValue(data) {
      this.cycleVal = data;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.cycleVal = '';
      this.handleQuery();
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('/examine/user/export', {
        ...this.queryParams
      }, '绩效考核人员数据导出.xlsx')
    },
    // 季度选择回调
    getQuarter(val) {
      this.queryParams.pickCycle = val
    },
    // 半年度选择回调
    getHalfyear(val) {
      this.queryParams.pickCycle = val
    }
  }
};
</script>
